<?php require_once('../Connections/sepolwan_connect.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable sesi_nama set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "index.php?stat=login_heula";
if (!((isset($_SESSION['sesi_nama'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['sesi_nama'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO artikel (id, judul, image, desk, isi, waktu, comment, view, jenis) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['id'], "int"),
                       GetSQLValueString($_POST['judul'], "text"),
                       GetSQLValueString($image=date("YmdHis")._.$_FILES['image']['name'], "text"),
                       GetSQLValueString($_POST['desk'], "text"),
                       GetSQLValueString($_POST['isi'], "text"),
                       GetSQLValueString($_POST['waktu'], "date"),
					   GetSQLValueString("0", "text"),
					   GetSQLValueString("0", "text"),
					   GetSQLValueString($_GET['jenis'], "text"));

define ("MAX_SIZE","400");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;
  
 if($_SERVER["REQUEST_METHOD"] == "POST")
 {
 	$image=$image;
	$uploadedfile = $_FILES['image']['tmp_name'];
     
 
 	if ($image) 
 	{
 	
 		$filename = stripslashes($image);
 	
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
		
		
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
		
 			$change='<div class="msgdiv">Unknown Image extension </div> ';
 			$errors=1;
 		}
 		else
 		{

 $size=filesize($_FILES['image']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
	$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
	$errors=1;
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);


$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);


$newwidth1=150;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);


$filename = "../images/artikel/".$image;

$filename1 = "../images/artikel/small/".$image;



imagejpeg($tmp,$filename,100);

imagejpeg($tmp1,$filename1,100);

imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}

}
  mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
  $Result1 = mysql_query($insertSQL, $sepolwan_connect) or die(mysql_error());

  $insertGoTo = "artikel.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_sepolwan_connect, $sepolwan_connect);
$query_view_artikel = "SELECT * FROM artikel WHERE jenis = '".$_GET['jenis']."' ORDER BY id DESC";
$view_artikel = mysql_query($query_view_artikel, $sepolwan_connect) or die(mysql_error());
$row_view_artikel = mysql_fetch_assoc($view_artikel);
$totalRows_view_artikel = mysql_num_rows($view_artikel);

$page_row = 4;
	$start = $_GET['start'];
	$start = ($start == '') ? '0' : $start; 
	if ($start >= $totalRows_view_artikel) 
	{
		$start = 0;
		$prev = 0;
		$next = $page_row;
	} elseif ($start >= 0) 
	{
		$start = $start;
		$prev = $start - $page_row;
		$next = $start + $page_row;
	} else {
		$start = 0;
		$prev = 0;
		$next = $page_row;
	} 
	$query_view_artikel = "SELECT * FROM artikel WHERE jenis = '".$_GET['jenis']."' ORDER BY id DESC limit $start, $page_row";
	$view_artikel = mysql_query($query_view_artikel, $sepolwan_connect) or die(mysql_error());
	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="ck/ckeditor.js"></script>
<script src="ck/sample.js"></script>
<link href="ck/sample.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
<link href="assets/outputxhtml/outputxhtml.css" rel="stylesheet">

</head>

<body>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td class="btnews_bg"><div class="txt14coklatbold" style="margin:10px 20px 10px 20px">Input Berita <?php echo $_GET['jenis'] ?></div></td>
  </tr>
  <tr>
    <td>
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1" enctype="multipart/form-data">
        <table width="100%" align="center">
          <tr valign="baseline">
            <td align="center"><strong style="color:#F00"><br />
            INPUT ARTIKEL</strong></td>
          </tr>
          <tr valign="baseline">
            <td><input type="text" name="judul" id="nama2" value="" size="32" placeholder="Judul <?php echo $_GET['jenis'] ?> . . " /></td>
          </tr>
          <tr valign="baseline">
            <td><div style="margin-left:5px">Image : <input type="file" name="image" value="" size="32" /></div></td>
          </tr>
          <tr valign="baseline">
            <td><textarea name="desk" cols="50" id="nama3" rows="5" placeholder="Deskripsi Artikel . ."></textarea></td>
          </tr>
          <tr valign="baseline">
            <td><textarea name="isi" id="editor1" cols="100%" rows="5"></textarea>
            <script>

				CKEDITOR.replace( 'editor1', {
					/*
					 * Style sheet for the contents
					 */
					contentsCss: 'assets/outputxhtml/outputxhtml.css',

					/*
					 * Special allowed content rules for spans used by
					 * font face, size, and color buttons.
					 *
					 * Note: all rules have been written separately so
					 * it was possible to specify required classes.
					 */
					extraAllowedContent: 'span(!FontColor1);span(!FontColor2);span(!FontColor3);' +
						'span(!FontColor1BG);span(!FontColor2BG);span(!FontColor3BG);' +
						'span(!FontComic);span(!FontCourier);span(!FontTimes);' +
						'span(!FontSmaller);span(!FontLarger);span(!FontSmall);span(!FontBig);span(!FontDouble)',

					/*
					 * Core styles.
					 */
					coreStyles_bold: {
						element: 'span',
						attributes: { 'class': 'Bold' }
					},
					coreStyles_italic: {
						element: 'span',
						attributes: { 'class': 'Italic' }
					},
					coreStyles_underline: {
						element: 'span',
						attributes: { 'class': 'Underline' }
					},
					coreStyles_strike: {
						element: 'span',
						attributes: { 'class': 'StrikeThrough' },
						overrides: 'strike'
					},
					coreStyles_subscript: {
						element: 'span',
						attributes: { 'class': 'Subscript' },
						overrides: 'sub'
					},
					coreStyles_superscript: {
						element: 'span',
						attributes: { 'class': 'Superscript' },
						overrides: 'sup'
					},

					/*
					 * Font face.
					 */

					// List of fonts available in the toolbar combo. Each font definition is
					// separated by a semi-colon (;). We are using class names here, so each font
					// is defined by {Combo Label}/{Class Name}.
					font_names: 'Comic Sans MS/FontComic;Courier New/FontCourier;Times New Roman/FontTimes',

					// Define the way font elements will be applied to the document. The "span"
					// element will be used. When a font is selected, the font name defined in the
					// above list is passed to this definition with the name "Font", being it
					// injected in the "class" attribute.
					// We must also instruct the editor to replace span elements that are used to
					// set the font (Overrides).
					font_style: {
						element: 'span',
						attributes: { 'class': '#(family)' },
						overrides: [
							{
								element: 'span',
								attributes: {
									'class': /^Font(?:Comic|Courier|Times)$/
								}
							}
						]
					},

					/*
					 * Font sizes.
					 */
					fontSize_sizes: 'Smaller/FontSmaller;Larger/FontLarger;8pt/FontSmall;14pt/FontBig;Double Size/FontDouble',
					fontSize_style: {
						element: 'span',
						attributes: { 'class': '#(size)' },
						overrides: [
							{
								element: 'span',
								attributes: {
									'class': /^Font(?:Smaller|Larger|Small|Big|Double)$/
								}
							}
						]
					} ,

					/*
					 * Font colors.
					 */
					colorButton_enableMore: false,

					colorButton_colors: 'FontColor1/FF9900,FontColor2/0066CC,FontColor3/F00',
					colorButton_foreStyle: {
						element: 'span',
						attributes: { 'class': '#(color)' },
						overrides: [
							{
								element: 'span',
								attributes: {
									'class': /^FontColor(?:1|2|3)$/
								}
							}
						]
					},

					colorButton_backStyle: {
						element: 'span',
						attributes: { 'class': '#(color)BG' },
						overrides: [
							{
								element: 'span',
								attributes: {
									'class': /^FontColor(?:1|2|3)BG$/
								}
							}
						]
					},

					/*
					 * Indentation.
					 */
					indentClasses: [ 'Indent1', 'Indent2', 'Indent3' ],

					/*
					 * Paragraph justification.
					 */
					justifyClasses: [ 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyFull' ],

					/*
					 * Styles combo.
					 */
					stylesSet: [
						{ name: 'Strong Emphasis', element: 'strong' },
						{ name: 'Emphasis', element: 'em' },

						{ name: 'Computer Code', element: 'code' },
						{ name: 'Keyboard Phrase', element: 'kbd' },
						{ name: 'Sample Text', element: 'samp' },
						{ name: 'Variable', element: 'var' },

						{ name: 'Deleted Text', element: 'del' },
						{ name: 'Inserted Text', element: 'ins' },

						{ name: 'Cited Work', element: 'cite' },
						{ name: 'Inline Quotation', element: 'q' }
					]
				});

			</script></td>
          </tr>
          <tr valign="baseline">
            <td><input type="submit" id="masuk" value="Simpan" /></td>
          </tr>
        </table>
        <input type="hidden" name="id" value="" />
        <input type="hidden" name="waktu" value="<?php echo date("y-m-d") ?>" />
        <input type="hidden" name="MM_insert" value="form1" />
      </form>
    </td>
  </tr>
</table>
<p align="center"><strong style="color:#F00">LIST BERITA <?php echo $_GET['jenis'] ?></strong></p>

<table border="1" cellpadding="5" cellspacing="0" width="100%" >
 
 <?php  
	  $num = $start;
	  while ($row_view_artikel = mysql_fetch_assoc($view_artikel)){
?>
 <tr>
   <td width="85%" valign="top"><img src="../images/artikel/small/<?php echo $row_view_artikel['image']; ?>" width="112" height="56" style="float:left" /><div style="margin-left:117px"> 
   <strong><?php echo $row_view_artikel['judul']; ?></strong><br />
<?php echo $row_view_artikel['desk']; ?></div></td>
   <td width="15%" align="center" valign="middle">view | <a href="editartikel.php?start=<?php echo $_GET['start'] ?>&amp;id=<?php echo $row_view_artikel['id']; ?>&jenis=<?php echo $_GET['jenis'] ?>">edit</a> | <a href="delartikel.php?start=<?php echo $_GET['start'] ?>&id=<?php echo $row_view_artikel['id']; ?>&jenis=<?php echo $_GET['jenis'] ?>">delete</a></td>
  </tr>
            <?php
}
?></table>
  <div align="center"> <table width="103" border="0" align="center" style="margin:10px">
              <tr>
                <?php if ( $prev > -1 and $totalRows_view_artikel > 0 ){ ?><td align="center" id="judul" ><div id="next" align="center"><a href="?start=<?php echo $prev ?>">Pref</a></div></td><?php } ?>
                <?php
			for($i = 0; $i < (round($totalRows_view_artikel / $page_row)); $i++)
				echo "<td align='center' id='judul'> [<a href=\"?start=".($i * $page_row)." \">".($i  + 1)."</a>] </td>";
			?>
                <?php if ( $totalRows_view_artikel > $next ){ ?><td id="judul"><div id="next" align="center"><a href="?start=<?php echo $next ?>">Next</a></div></td><?php } ?>
                </tr>
                </table></div>
</body>
</html>
<?php
mysql_free_result($view_artikel);
?>
